package com.westcom.vulb.protocal.http;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.sax.StartElementListener;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


public class Response {
    public static final int 
            SUCCESS             = 0,
            SYSTEM_ERROR        = 1,
            PARAM_LOGIC_ERROR   = 2,
            USER_LOCKED         = 3,
            USER_NOT_CONFIRM    = 4,    //?
            SYSTEM_MAINTANANCE  = 5,
            SESSION_ERROR       = 6,
            LOCAL_ERROR			= 7,
            
    
            ERROR               = 1001,
            HTTP_ERROR          = 1002,
            
            WS_ERROR			= 1003,
            
            unsed               = -1;
    
    private static Type HASHMAP_TYPE = new TypeToken<HashMap<String, String>>(){}.getType();
    
    public int errno;
    public String errmsg;
    public HashMap<String, Object> data;
    public HashMap<String, Object> action;
    public String mOriginalStr = null;
    
    public static Response DEFAULT_ERROR_RESPONSE = build(ERROR, "");
    
    public static Response build(int errno, String errmsg) {
        Response response = new Response();
        
        response.errno = errno;
        response.errmsg = errmsg;
        response.data = new HashMap<String, Object>();
        response.action = new HashMap<String,Object>();
        
        return response;
    }
    
    public static Response parse(String json) {
        if (json == null) 
        {
            return DEFAULT_ERROR_RESPONSE;
        }
        JSONObject js = null;
		try 
		{
			js = new JSONObject(json);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return DEFAULT_ERROR_RESPONSE;
		}
        HashMap<String, Object> map = json2map(js);
        
        if (map == null || !map.containsKey("result")) {
            return DEFAULT_ERROR_RESPONSE;
        }
        
        HashMap<String, String> statusInfo = (HashMap<String, String>) map.get("result"); 
        
        if (statusInfo == null || 
                !statusInfo.containsKey("statusCode") || 
                !statusInfo.containsKey("message")) {
            return DEFAULT_ERROR_RESPONSE;
        }
        
        int errno = Integer.valueOf(statusInfo.get("statusCode"));
        String errmsg = statusInfo.get("message").toString();
        
        Object a = map.get("action");
        HashMap<String, Object> action = null;
        if(a instanceof HashMap)
        {
        	action = (HashMap<String, Object>)a;
        }
        
        Object d = map.get("data");
        HashMap<String, Object> data = null;
        if(d instanceof HashMap)
        {
        	data = (HashMap<String, Object>)d;
        }
        
        Response ret = build(errno, errmsg);
        ret.data = data;
        ret.action = action;
        ret.mOriginalStr = json;
        return ret;
    }
    public static HashMap<String, Object> json2map(JSONObject js)
    {
    	HashMap<String, Object> map = new HashMap<String, Object>();
    	try 
    	{
	    	Iterator<String>keys = js.keys();
	    	String key;
	    	Object o = null;
	    	while(keys.hasNext())
	    	{
	    		key = keys.next();
	    		o = js.get(key);
	    		if(o instanceof JSONObject)
	    		{
	    			JSONObject j = (JSONObject)o;
	    			HashMap<String, Object> m = json2map(j);
	    			map.put(key, m);
	    		}else
	    		{
	    			map.put(key, o);
	    		}
	    	}
    	} catch (JSONException e) 
    	{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return map;
    }
    
    public static Response parse2(String json) {
        if (json == null) {
            return DEFAULT_ERROR_RESPONSE;
        }
        
//        Gson gson = new Gson();
//        HashMap<String, Object> map = gson.fromJson(json, HASHMAP_TYPE);
        
        Map<String, Object> map = new HashMap<String, Object>();
        ObjectMapper mapper = new ObjectMapper();
        try {
            //convert JSON string to Map
            map = mapper.readValue(json, new TypeReference<HashMap<String, Object>>(){});
        } catch (Exception e) {
            //TODO process parse error
            e.printStackTrace();
        }
        
        if (map == null || !map.containsKey("result")) {
            return DEFAULT_ERROR_RESPONSE;
        }
        
        HashMap<String, String> statusInfo = (HashMap<String, String>) map.get("result"); 
        if (statusInfo == null || 
                !statusInfo.containsKey("statusCode") || 
                !statusInfo.containsKey("message")) {
            return DEFAULT_ERROR_RESPONSE;
        }
        
        int errno = Integer.valueOf(statusInfo.get("statusCode"));
        String errmsg = statusInfo.get("message").toString();
        
        HashMap<String, Object> data = (HashMap<String, Object>) map.get("data");
        if (data == null) {
            data = new HashMap<String, Object>();
        }
        
        Response ret = build(errno, errmsg);
        ret.data = data;
        
        return ret;
    }
}